package haco.syncremotefile.service;

import haco.syncremotefile.dao.SyncLogDao;
import haco.syncremotefile.entity.SyncLog;
import haco.syncremotefile.modal.STATUS;
import haco.syncremotefile.utils.SourceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @author wangyuwei
 * @history 2021-06-09 09:03
 */
@Service
public class SyncLogService {
    @Autowired
    SyncLogDao syncLogDao;

    public SyncLog insert(SyncLog syncLog){

        return syncLogDao.save(syncLog);
    }
    public SyncLog insertLog(SourceConfig.Task task, String fullName , STATUS status){
        SyncLog syncLog = new SyncLog();
        syncLog.setSourceIp(task.getSource().getIp());
        syncLog.setSourceDir(task.getSource().getDir());
        syncLog.setSourceFilename(fullName);
        syncLog.setTargetDir(task.getTarget().getDir() + fullName);
        syncLog.setTargetFilename(fullName);
        syncLog.setStatus(status.getCode());
        syncLog.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        syncLog.setId(UUID.randomUUID().toString());
        return syncLogDao.save(syncLog);
    }
}
